<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: LoginService.cfc 865 2006-07-05 03:08:34Z mpwoodward $

Notes:
--->
<cfcomponent displayname="LoginService" output="false" hint="LoginService for MachBlog">
	<cfproperty name="loginGateway" type="LoginGateway" required="true" />
	<cfproperty name="notificationService" type="machblog.org.machblog.notification.NotificationService" required="true" />
	<cfproperty name="passwordService" type="PasswordService" required="true" />
	
	<cffunction name="init" access="public" output="false" returntype="LoginService" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setLoginGateway" access="public" output="false" returntype="void">
		<cfargument name="loginGateway" type="LoginGateway" required="true" />
		<cfset variables.loginGateway = arguments.loginGateway />
	</cffunction>
	<cffunction name="getLoginGateway" access="public" output="false" returntype="LoginGateway">
		<cfreturn variables.loginGateway />
	</cffunction>
	
	<cffunction name="setNotificationService" access="public" output="false" returntype="void">
		<cfargument name="notificationService" type="machblog.org.machblog.notification.NotificationService" required="true" />
		<cfset variables.notificationService = arguments.notificationService />
	</cffunction>
	<cffunction name="getNotificationService" access="public" output="false" returntype="machblog.org.machblog.notification.NotificationService">
		<cfreturn variables.notificationService />
	</cffunction>
	
	<cffunction name="setPasswordService" access="public" output="false" returntype="void">
		<cfargument name="passwordService" type="PasswordService" required="true" />
		<cfset variables.passwordService = arguments.passwordService />
	</cffunction>
	<cffunction name="getPasswordService" access="public" output="false" returntype="PasswordService">
		<cfreturn variables.passwordService />
	</cffunction>
	
	<cffunction name="validateLogin" access="public" output="false" returntype="boolean" 
			hint="Validates a login attempt and returns a boolean indicating the success of failure">
		<cfargument name="email" type="string" required="true" />
		<cfargument name="password" type="string" required="true" />
		
		<cfset var hashedPassword = getPasswordService().saltAndHashPassword(arguments.email, arguments.password) />
		
		<cfreturn getLoginGateway().validateLogin(arguments.email, hashedPassword) />
	</cffunction>
	
	<cffunction name="checkEmailExists" access="public" output="false" returntype="boolean" 
			hint="Checks to see if the email address passed in exists as a user in the system">
		<cfargument name="email" type="string" required="true" />
		
		<cfreturn getLoginGateway().checkEmailExists(arguments.email) />
	</cffunction>
	
	<cffunction name="getUserIdByEmailAddress" access="public" output="false" returntype="string" 
			hint="Returns the user ID corresponding to the email address passed in">
		<cfargument name="email" type="string" required="true" />
		
		<cfreturn getLoginGateway().getUserIdByEmailAddress(arguments.email) />
	</cffunction>
	
	<cffunction name="resetPassword" access="public" output="false" returntype="void" 
			hint="Generates a new random password for a user and emails it to the user">
		<cfargument name="userId" type="uuid" required="true" />
		<cfargument name="toEmail" type="string" required="true" />
		<cfargument name="fromEmail" type="string" required="true" />
		<cfargument name="emailSubject" type="string" required="true" />
		<cfargument name="emailBody" type="string" required="true" />
		
		<cfset var newPassword = getPasswordService().generatePassword() />
		<cfset var hashedPassword = getPasswordService().saltAndHashPassword(arguments.toEmail, newPassword) />
		
		<cfset getLoginGateway().updatePassword(arguments.userId, hashedPassword) />
		
		<cfset getNotificationService().emailNewPassword(arguments.toEmail, arguments.fromEmail, arguments.emailSubject, arguments.emailBody, 
															newPassword) />
	</cffunction>
	
	<cffunction name="logout" access="public" output="false" returntype="void" 
			hint="Logs the user out">
		<cfset getLoginGateway().logout() />
	</cffunction>
</cfcomponent>
